﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using System.Collections.Specialized;
using ASPMVCUtil.Util;

namespace ASPMVCUtil.Helpers.Html.Grid
{
    /// <summary>
    /// Grid parametrizer
    /// </summary>
    public class GridParams
    {
        /// <summary>
        /// OrderBy field
        /// </summary>
        public string OrderField { get; private set; }
        /// <summary>
        /// Order direction
        /// </summary>
        public string OrderDirection { get; private set; }
        /// <summary>
        /// OrderBy in DB format
        /// </summary>
        public string OrderBySpace
        {
            get
            {
                return (OrderField != null && OrderDirection != null) ? OrderField + " " + OrderDirection : null;
            }
        }
        /// <summary>
        /// Query string requested
        /// </summary>
        public NameValueCollectionRouteValueDictionary QueryString { get; set; }
        /// <summary>
        /// Current showed page
        /// </summary>
        public int CurrentPage { get; private set; }
        /// <summary>
        /// Grid id
        /// </summary>
        public string ID { get; private set; }
        /// <summary>
        /// Father grid id
        /// </summary>
        public string FatherID { get; private set; }
        /// <summary>
        /// Selected row id
        /// </summary>
        public string SelectedRowID { get; private set; }

        /// <summary>
        /// Builds a grid paramitrizer from requested QueryString
        /// </summary>
        /// <param name="gridId">Grid id</param>
        /// <param name="QueryString">Requested QueryString</param>
        /// <param name="fhaterID">Optional. Father grid id</param>
        /// <returns></returns>
        public static GridParams create(string gridId, NameValueCollectionRouteValueDictionary QueryString, string fhaterID = null)
        {
            var gridParams = new GridParams();
            gridParams.FatherID = fhaterID;
            gridParams.QueryString = QueryString;
            gridParams.ID = gridId;
            if (gridParams.QueryString[fatherParamString(gridId)] != null)
            {
                gridParams.CurrentPage = Convert.ToInt32(gridParams.QueryString[fatherParamString(gridId)]);
            }
            else
            {
                gridParams.CurrentPage = 1;
            }
            if (gridParams.QueryString[orderByParamString(gridId)] != null)
            {
                var orderBy = gridParams.QueryString[orderByParamString(gridId)].ToString().Split('-');
                gridParams.OrderField = orderBy[0];
                gridParams.OrderDirection = orderBy[1];
                if (gridParams.OrderDirection != GridOrderDirection.asc.ToString() && gridParams.OrderDirection != GridOrderDirection.desc.ToString())
                    throw new ArgumentException("Order diection has to be asc or desc");
            }
            if (gridParams.QueryString[iDParamString(gridId)] != null)
            {
                gridParams.SelectedRowID = gridParams.QueryString[iDParamString(gridId)].ToString();
            }
            return gridParams;
        }

        public static string iDParamString(string gridId)
        {
            return gridId + "_ID";
        }

        public static string orderByParamString(string gridId)
        {
            return gridId + "_OrderBy";
        }

        public static string fatherParamString(string gridId)
        {
            return gridId + "_p";
        }
    }
}